/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common/rs_common_hook.h"

namespace OHOS::Rosen {
RsCommonHook& RsCommonHook::Instance()
{
    static RsCommonHook rch;
    return rch;
}

void RsCommonHook::RegisterStartNewAnimationListener(std::function<void(const std::string &)> listener)
{
    startNewAniamtionFunc_ = listener;
}

void RsCommonHook::OnStartNewAnimation(const std::string &componentName)
{
    if (startNewAniamtionFunc_) {
        startNewAniamtionFunc_(componentName);
    }
}

void RsCommonHook::SetVideoSurfaceFlag(bool videoSurfaceFlag)
{
    videoSurfaceFlag_ = videoSurfaceFlag;
}

bool RsCommonHook::GetVideoSurfaceFlag() const
{
    return videoSurfaceFlag_;
}

// use to implement product isolation for the adaptive P3 scheme
void RsCommonHook::SetAdaptiveColorGamutEnable(bool isAdaptiveColorGamutEnable)
{
    isAdaptiveColorGamutEnable_ = isAdaptiveColorGamutEnable;
}

bool RsCommonHook::IsAdaptiveColorGamutEnabled() const
{
    return isAdaptiveColorGamutEnable_;
}

// skip hwcnode hardware state updating
void RsCommonHook::SetHardwareEnabledByHwcnodeBelowSelfInAppFlag(bool hardwareEnabledByHwcnodeSkippedFlag)
{
    hardwareEnabledByHwcnodeSkippedFlag_ = hardwareEnabledByHwcnodeSkippedFlag;
}

void RsCommonHook::SetHardwareEnabledByBackgroundAlphaFlag(bool hardwareEnabledByBackgroundAlphaSkippedFlag)
{
    hardwareEnabledByBackgroundAlphaSkippedFlag_ = hardwareEnabledByBackgroundAlphaSkippedFlag;
}

bool RsCommonHook::GetHardwareEnabledByHwcnodeBelowSelfInAppFlag() const
{
    return hardwareEnabledByHwcnodeSkippedFlag_;
}

bool RsCommonHook::GetHardwareEnabledByBackgroundAlphaFlag() const
{
    return hardwareEnabledByBackgroundAlphaSkippedFlag_;
}

bool RsCommonHook::GetIsWhiteListForSolidColorLayerFlag() const
{
    return isWhiteListForSolidColorLayerFlag_;
}
    
void RsCommonHook::SetIsWhiteListForSolidColorLayerFlag(bool isWhiteListForSolidColorLayerFlag)
{
    isWhiteListForSolidColorLayerFlag_ = isWhiteListForSolidColorLayerFlag;
}

void RsCommonHook::SetComponentPowerFpsFunc(std::function<void(FrameRateRange& range)> func)
{
    componentPowerFpsFunc_ = func;
}

void RsCommonHook::GetComponentPowerFps(FrameRateRange& range)
{
    if (componentPowerFpsFunc_) {
        componentPowerFpsFunc_(range);
    }
}

// DISPLAY ENGINE
void RsCommonHook::SetCurrentPkgName(const std::string& pkgName)
{
    pkgName_ = pkgName;
}

std::string RsCommonHook::GetCurrentPkgName() const
{
    return pkgName_;
}

void RsCommonHook::SetImageEnhancePidList(const std::unordered_set<pid_t>& imageEnhancePidList)
{
    std::lock_guard<std::mutex> setMutex(mutexLock_);
    imageEnhancePidList_ = imageEnhancePidList;
}

std::unordered_set<pid_t> RsCommonHook::GetImageEnhancePidList() const
{
    std::lock_guard<std::mutex> setMutex(mutexLock_);
    return imageEnhancePidList_;
}

void RsCommonHook::SetImageEnhanceParams(const RSImageDetailEnhanceParams& imageEnhanceParams)
{
    std::lock_guard<std::mutex> setMutex(mutexLock_);
    imageEnhanceParams_ = imageEnhanceParams;
}

RSImageDetailEnhanceParams RsCommonHook::GetImageEnhanceParams() const
{
    std::lock_guard<std::mutex> setMutex(mutexLock_);
    return imageEnhanceParams_;
}

void RsCommonHook::SetImageEnhanceAlgoParams(
    const std::unordered_map<std::string, RSImageDetailEnhanceAlgoParams>& imageEnhanceAlgoParams)
{
    std::lock_guard<std::mutex> setMutex(mutexLock_);
    imageEnhanceAlgoParams_ = imageEnhanceAlgoParams;
}

RSImageDetailEnhanceAlgoParams RsCommonHook::GetImageEnhanceAlgoParams(const std::string& key) const
{
    std::lock_guard<std::mutex> setMutex(mutexLock_);
    auto it = imageEnhanceAlgoParams_.find(key);
    if (it != imageEnhanceAlgoParams_.end()) {
        return it->second;
    }
    return {};
}

bool RsCommonHook::IsImageEnhanceParamsValid()
{
    if (imageEnhanceAlgoParams_.find("SLR") == imageEnhanceAlgoParams_.end() ||
        imageEnhanceAlgoParams_.find("ESR") == imageEnhanceAlgoParams_.end()) {
        return false;
    }
    if (imageEnhanceParams_.isValid && imageEnhanceAlgoParams_["SLR"].isValid &&
        imageEnhanceAlgoParams_["ESR"].isValid) {
        return true;
    }
    return false;
}

void RsCommonHook::SetTvPlayerBundleName(const std::string& bundleName)
{
    tvPlayerBundleName_ = bundleName;
}

const std::string& RsCommonHook::GetTvPlayerBundleName() const
{
    return tvPlayerBundleName_;
}

void RsCommonHook::SetSolidColorLayerConfigFromHgm(
    const std::unordered_map<std::string, std::string>& solidLayerConfigFromHgm)
{
    solidLayerConfigFromHgm_ = solidLayerConfigFromHgm;
}

bool RsCommonHook::IsSolidColorLayerConfig(const std::string& bundleName)
{
    return solidLayerConfigFromHgm_.find(bundleName) != solidLayerConfigFromHgm_.end();
}

void RsCommonHook::SetHwcSolidColorLayerConfigFromHgm(
    const std::unordered_map<std::string, std::string>& hwcSolidLayerConfigFromHgm)
{
    hwcSolidLayerConfigFromHgm_ = hwcSolidLayerConfigFromHgm;
}

bool RsCommonHook::IsHwcSolidColorLayerConfig(const std::string& bundleName)
{
    return hwcSolidLayerConfigFromHgm_.find(bundleName) != hwcSolidLayerConfigFromHgm_.end();
}

void RsCommonHook::SetFilterUnderHwcConfigByApp(const std::string& appName, const std::string& val)
{
    filterUnderHwcConfig_[appName] = val;
}

std::string_view RsCommonHook::GetFilterUnderHwcConfigByApp(const std::string& appName)
{
    if (auto it = filterUnderHwcConfig_.find(appName); it != filterUnderHwcConfig_.end()) {
        return it->second;
    }
    return "";
}

void RsCommonHook::SetOverlappedHwcNodeInAppEnabledConfig(const std::string& appName, const std::string& val)
{
    overlappedHwcNodeInAppEnabledConfig_.insert_or_assign(appName, val);
}

std::string RsCommonHook::GetOverlappedHwcNodeInAppEnabledConfig(const std::string& appName)
{
    if (auto it = overlappedHwcNodeInAppEnabledConfig_.find(appName);
        it != overlappedHwcNodeInAppEnabledConfig_.end()) {
        return it->second;
    }
    return "";
}
} // namespace OHOS::Rosen